package mian.java.concurrency.simpleDateFormat;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * 线程安全的时间格式化
 */
public class SafeDateThread extends Thread{

    /* 要转换的日期字符串 */
    private String dateString;

    public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    public static LocalDate parse(String dateString){
        return LocalDate.parse(dateString, DATE_TIME_FORMATTER);
    }

    public static String format(LocalDate target) {
        return target.format(DATE_TIME_FORMATTER);
    }

    public SafeDateThread(String dateString){
        this.dateString = dateString;
    }

    @Override
    public void run() {
        try {
            String newDate = this.format(this.parse(dateString));
            System.out.println("ThreadName=" + this.getName()
                    + " 原来日期字符串：" + dateString
                    + " 转换成的日期为：" + newDate);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
